<?php
/**
 * Created by PhpStorm.
 * User: khanhht
 * Date: 7/16/14
 * Time: 11:29 AM
 */

class My_Service_Channel extends My_Service_Abstract {


    public function __construct(){
        parent::__construct();
        $this->_model = new Default_Model_Channel();
    }

    /**
     * Get channel of game
     * @param $gameId
     * @param null $userId
     * @param null $openDate
     * @param null $closeDate
     * @return array|mixed
     */
    public function getChannelGame($gameId, $userId = null, $openDate = null, $closeDate = null, $special = null){
        /* Build cache key based on params */
        $key = sprintf($this->_cache_key,md5(__CLASS__ . __FUNCTION__ . json_encode(func_get_args())));
        /* Get data from cache */
        //$result = $this->_memcache->get($key);
        /* Cache missed */
        if(!isset($result) || !$result){
            $result = $this->_model->getChannelGame($gameId,$openDate,$closeDate,$special)->toArray();
            /* Nếu có param $userId thì chỉ lấy các channel user được phép truy cập */
            if($userId){
                $disabledChannel = $this->_model->getDisabledChannel($userId, $gameId);
                for( $i = 0 ; $i < count($result) ; $i++){
                    if(in_array($result[$i]['channelId'],$disabledChannel)){
                        unset($result[$i]);
                    }
                }

            }
            $this->_memcache->set($key,$result,false,0);
        }
        return $result;
    }


    /**
     * get servers of channel
     * @param $gameId
     * @param $channelId
     * @return array|mixed
     */
    public function getServerChannelGame($gameId,$channelId){

        $key = sprintf($this->_cache_key,md5(__CLASS__ . __FUNCTION__ . json_encode(func_get_args())));

        $result = $this->_memcache->get($key);
        if(!$result){
            $result = $this->_model->getServerChannel($gameId,$channelId)->toArray();
            $this->_memcache->set($key,$result,false,0);
        }
        return $result;

    }

} 